public class Test42 {
    //范围内的字符进行翻转
    public static void reverse(char[] data,int start,int end){
        if (data == null || data.length == 0 || start < 0|| end > data.length-1 || start > end) return;

        while (start < end){
            char temp = data[start];
            data[start]  = data[end];
            data[end] = temp;
            start ++;
            end --;
        }
    }

    public static char[] reverseSentence(char[] data){
        if (data == null || data.length ==0) return  data;
        reverse(data,0,data.length-1); //翻转全部的字符

        int start = 0;
        int end = 0;

        while (start < data.length){
            if (data[start] == ' '){
                start ++;
                end ++;
            }else if (end == data.length || data[end] == ' '){
                reverse(data,start,end-1);//翻转单个单词
                end ++;
                start = end;
            }else end ++;
        }
        return data;
    }

    public static char[] leftRotateString(char[] data,int n){
        if (data == null || data.length == 0 || n < 0 || n >data.length) return data;
        reverse(data,0,n-1);
        reverse(data,n,data.length-1);
        reverse(data,0,data.length-1);
        return data;
    }
    public static void main(String[] args) {
        System.out.println(new String(reverseSentence("I am a student.".toCharArray())));
        System.out.println(new String(reverseSentence("Wonderful".toCharArray())));


        System.out.println(new String(leftRotateString("abcdefg".toCharArray(), 2)));
        System.out.println(new String(leftRotateString("abcdefg".toCharArray(), 1)));
        System.out.println(new String(leftRotateString("abcdefg".toCharArray(), 6)));
        System.out.println(new String(leftRotateString("abcdefg".toCharArray(), 7)));
        System.out.println(new String(leftRotateString("abcdefg".toCharArray(), 0)));

    }
}
